package com.qf.j2102.controller;

import com.qf.j2102.common.JsonBean;
import com.qf.j2102.po.SysAdmin;
import com.qf.j2102.po.SysCar;
import com.qf.j2102.po.SysOrder;
import com.qf.j2102.po.SysUser;
import com.qf.j2102.service.SysAdminService;
import com.qf.j2102.utils.JsonUtils;
import com.qf.j2102.utils.MD5;
import com.qf.j2102.utils.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    @Qualifier("sysAdminService")
    private SysAdminService adminService;

    /**
     * 后台登录
     * @param a_uname
     * @param a_pword
     * @return
     */
    @RequestMapping(value = "/login.do",method = RequestMethod.POST)
    @ResponseBody
    public JsonBean login(String a_uname, String a_pword, HttpSession session) {
        JsonBean bean = null;
        //MD5 md5 = new MD5();
       // String pass = md5.getMD5ofStr(a_pword);
        try {
            SysAdmin sysAdmin = adminService.login(a_uname, a_pword);
            if (sysAdmin!=null)
                System.out.println(sysAdmin);
                session.setAttribute("admin",sysAdmin);
                bean = JsonUtils.createJsonBean(0,sysAdmin);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(1,e.getMessage());
        }
        return bean;
    }

    /**
     * 修改个人信息
     * @param admin
     * @param session
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonBean update(SysAdmin admin, HttpSession session) {
        JsonBean bean = null;
        int update = adminService.update(admin);
        if (update==1){
            session.setAttribute("admin",admin);
            bean=JsonUtils.createJsonBean(1,admin);
            System.out.println(admin);
        }else {
            bean=JsonUtils.createJsonBean(0,"cuowu");
        }
        return bean ;
    }
    /**
     * 后台注册
     * @param sysAdmin
     * @return
     */
    @RequestMapping("/regist.do")
    @ResponseBody
    public JsonBean regist(SysAdmin sysAdmin) {
        JsonBean bean = null;
        MD5 md5 = new MD5();
        String md5ofStr = md5.getMD5ofStr(sysAdmin.getA_pword());
        sysAdmin.setA_pword(md5ofStr);
        try {
            adminService.regist(sysAdmin);
            bean = JsonUtils.createJsonBean(0,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(1,e.getMessage());
        }
        return bean;
    }

    /**
     * 查询所用用户信息
     * @return
     */
    @RequestMapping(value = "/allUsers.do",method = RequestMethod.GET)
    @ResponseBody
    public JsonBean allUsers() {
        JsonBean bean = null;
        try {
            List<SysUser> sysUsers = adminService.selectAllUsers();
            bean = JsonUtils.createJsonBean(0,sysUsers);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(1,e.getMessage());
        }
        return bean;
    }

    /**
     * 查询所有车辆信息
     * @return
     */
    @RequestMapping("/allCars.do")
    @ResponseBody
    public JsonBean allCars() {
        JsonBean bean = null;
        try {
            List<SysCar> sysCars = adminService.selectAllCars();
            bean = JsonUtils.createJsonBean(0,sysCars);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(1,e.getMessage());
        }
        return bean;
    }

    /**
     * 查询所有订单信息
     * @return
     */
    @RequestMapping("/allOrders.do")
    @ResponseBody
    public JsonBean allOrders() {
        JsonBean bean = null;
        try {
            List<SysOrder> sysOrders = adminService.selectAllOrders();
            bean = JsonUtils.createJsonBean(0,sysOrders);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(1,e.getMessage());
        }
        return bean;
    }
    /**
     * 批量删除用户信息
     * @return
     */
    @RequestMapping("/delUser.do")
    @ResponseBody
    public JsonBean delUser(List<Integer> ids) {
        JsonBean bean = null;
        try {
            int i = adminService.delUser(ids);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
    /**
     * 批量删除用户信息
     * @return
     */
    @RequestMapping("/delCar.do")
    @ResponseBody
    public JsonBean delCar(List<Integer> ids) {
        JsonBean bean = null;
        try {
            int i = adminService.delCar(ids);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
    /**
     * 批量删除用户信息
     * @return
     */
    @RequestMapping("/delOrder.do")
    @ResponseBody
    public JsonBean delOrder(List<Integer> ids) {
        JsonBean bean = null;
        try {
            int i = adminService.delOrder(ids);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
}
